package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;

/**
 * Load a fleet from a plain text file.
 * Apr 7, 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         LoadFleet.java
 */
public class LoadFleet extends AbstractAction {
    /**
     * The serial UID.
     */
    private static final long serialVersionUID = -7272210396134627492L;

    /**
     * Default constructor.
     */
    public LoadFleet() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.LOAD_FLEET));
        this.putValue(SMALL_ICON, ImagesDatabase.LOAD);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.LOAD_FLEET));
    }

    /**
     * Load a fleet from a file, the file is selected by a JFileChooser.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        File f;
        JFileChooser explorer;
        explorer = new JFileChooser(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.FOLDER_NAME_PUD_DATA));
        FileFilter filterText = new FileNameExtensionFilter(
                LanguagesConstants.LANGUAGE.getString(LanguagesConstants.FILE)
                        + ": fleet", "fleet");
        explorer.addChoosableFileFilter(filterText);
        explorer.setFileSelectionMode(JFileChooser.FILES_ONLY);
        /*
         * Getting the file
         */
        explorer.showDialog(
                null,
                LanguagesConstants.LANGUAGE.getString(LanguagesConstants.OPEN)
                        + " "
                        + LanguagesConstants.LANGUAGE
                                .getString(LanguagesConstants.FLEET));
        if (explorer.getSelectedFile() != null) {
            f = explorer.getSelectedFile();
            Fleet.getInstance().setFleet(Fleet.loadFleet(f));
        }
    }
}
